/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package io.iec.edp.caf.security.core.details;

import io.iec.edp.caf.security.core.GrantedAuthority;
import io.iec.edp.caf.security.core.UserDetailsSource;
import lombok.Builder;

import java.util.Collection;
import java.util.Locale;
import java.util.Map;
import java.util.Optional;


/**
 * This is {@link ServiceUser}.
 *
 * @author zhangtianyi
 * @since 1.0.0
 */
@Builder
public class ServiceUser implements User {

    private final String serviceId;

    private final String serviceCode;

    private final String serviceName;

    private Locale locale;

    private String customizedLocaleString;

    private Map<String, String> externalAttributes;

    @Override
    public String getId() {
        return serviceId;
    }

    @Override
    public String getCode() {
        return serviceCode;
    }

    @Override
    public String getName() {
        return serviceName;
    }

    @Override
    public Optional<String> getFullNameEn() {
        return Optional.empty();
    }

    @Override
    public void setFullNameEn(String fullNameEn) {

    }


    @Override
    public UserTypeEnum getType() {
        return UserTypeEnum.SERVICE;
    }

    @Override
    public Optional<Locale> getLocale() {
        return Optional.ofNullable(locale);
    }

    @Override
    public Optional<String> getTimezone() {
        return Optional.empty();
    }

    @Override
    public Optional<String> getCustomizedLocaleString() {
        return Optional.ofNullable(customizedLocaleString);
    }

    @Override
    public void setCustomizedLocaleString(String customizedLocaleString) {
        this.customizedLocaleString = customizedLocaleString;
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }

    @Override
    public UserDetailsSource getUserDetailsSource() {
        return null;
    }

    @Override
    public Optional<String> getExternalAttributeAsString(String attributeName) {

        return externalAttributes != null
                ? Optional.ofNullable(externalAttributes.get(attributeName))
                : Optional.empty();
    }

    @Override
    public void updateExternalAttribute(Map<String, String> attributes) {
        externalAttributes.putAll(attributes);
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return null;
    }

    @Override
    public PersonalUser toPersonalUser() throws UserTypeMismatchException {
        throw new UserTypeMismatchException(UserTypeEnum.SERVICE.toString());
    }

    @Override
    public ServiceUser toServiceUser() {
        return this;
    }


}
